package com.piece.core.mongo.context;

import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.util.object.BeanUtil;
import com.piece.core.log.client.LoginLogService;
import com.piece.core.log.client.dto.LoginLogDTO;
import com.piece.core.mongo.entity.MongoLoginLog;
import com.piece.core.mongo.service.MongoLoginLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component("login_log_service_mongo")
@ConditionalOnProperty(name = "spring.log.type", havingValue = "mongo")
public class LoginLogServiceMongoImpl implements LoginLogService {

    @Resource
    private MongoLoginLogService mongoLoginLogService;

    @Async
    @Override
    public LoginLogDTO login(LoginLogDTO logDTO) {
        try {
            MongoLoginLog loginLog = convertToDb(logDTO);
            loginLog = mongoLoginLogService.insert(loginLog);
            logDTO.setLogId(loginLog.getLogId());
            return logDTO;
        } catch (Exception e) {
            log.error("插入登录日志异常：{}", e.getMessage());
        }

        return null;
    }

    @Override
    public LoginLogDTO logout(LoginLogDTO logDTO) {
        try {
            MongoLoginLog loginLog = convertToDb(logDTO);
            loginLog = mongoLoginLogService.insert(loginLog);
            logDTO.setLogId(loginLog.getLogId());
            return logDTO;
        } catch (Exception e) {
            log.error("插入登出日志异常：{}", e.getMessage());
        }

        return null;
    }

    @Override
    public LoginLogDTO force(LoginLogDTO logDTO) {
        try {
            MongoLoginLog loginLog = convertToDb(logDTO);
            loginLog = mongoLoginLogService.insert(loginLog);
            logDTO.setLogId(loginLog.getLogId());
            return logDTO;
        } catch (Exception e) {
            log.error("插入强退日志异常：{}", e.getMessage());
        }

        return null;
    }

    @Override
    public void clear() {
        mongoLoginLogService.deleteAll();
    }

    @Override
    public List<LoginLogDTO> findByCondition(Map<String, Object> param) {
        QueryTable queryTable = new QueryTable();
        queryTable.setParams(param);
        List<MongoLoginLog> loginLogs = mongoLoginLogService.findList(queryTable);
        return loginLogs.stream().map(loginLog -> {
            LoginLogDTO audit = null;
            try {
                audit = convertToLog(loginLog);
            } catch (Exception e) {
                log.error("转换日志对象异常：{}", e.getMessage());
            } finally {
                return audit;
            }
        }).collect(Collectors.toList());
    }

    public DataTable<MongoLoginLog> findByCondition(QueryTable queryTable) {
        return mongoLoginLogService.findPage(queryTable);
    }

    private MongoLoginLog convertToDb(LoginLogDTO loginLog) {
        MongoLoginLog log = new MongoLoginLog();
        BeanUtil.copyProperties(loginLog, log);
        return log;
    }

    private LoginLogDTO convertToLog(MongoLoginLog log) {
        LoginLogDTO loginLog = new LoginLogDTO();
        BeanUtil.copyProperties(log, loginLog);
        return loginLog;
    }
}
